import { asyncRoutes } from '@/router/asyncRoutes'
import { menuDataToRouter } from '@/router/menuToRouter'
import { MenuListType } from '@/service/types/menu'
import { AxiosPromise } from 'axios'
import request from '@/hooks/utils/http/index'
import { RouteRecordRaw } from 'vue-router'
import { MenuTypeEnum } from '@/service/enums/appEnum'

interface MenuResponse {
  menuList: MenuListType[]
}

// 菜单接口
export const menuService = {
  async getMenuList(delay = 300): Promise<MenuResponse> {
    try {
      // 模拟接口返回的菜单数据
      const menuData = asyncRoutes
      // 处理菜单数据
      const menuList = menuData.map((route) => menuDataToRouter(route))
      // 模拟接口延迟
      await new Promise((resolve) => setTimeout(resolve, delay))

      return { menuList }
    } catch (error) {
      throw error instanceof Error ? error : new Error('获取菜单失败')
    }
  }
}

export class MenuService {
    // 获取路由
    static getRouters(): AxiosPromise<RouteRecordRaw[]> {
        return request({
            url: '/system/menu/getRouters',
            method: 'get'
        });
    }
    // 查询菜单列表
    static listMenu = (query?: MenuQuery): AxiosPromise<MenuVO[]> => {
        return request({
            url: '/system/menu/list',
            method: 'get',
            params: query
        });
    };

// 查询菜单详细
    static getMenu = (menuId: string | number): AxiosPromise<MenuVO> => {
        return request({
            url: '/system/menu/' + menuId,
            method: 'get'
        });
    };

// 查询菜单下拉树结构
    static treeselect = (): AxiosPromise<MenuTreeOption[]> => {
        return request({
            url: '/system/menu/treeselect',
            method: 'get'
        });
    };

// 根据角色ID查询菜单下拉树结构
    static roleMenuTreeselect = (roleId: string | number): AxiosPromise<RoleMenuTree> => {
        return request({
            url: '/system/menu/roleMenuTreeselect/' + roleId,
            method: 'get'
        });
    };

// 根据角色ID查询菜单下拉树结构
    static tenantPackageMenuTreeselect = (packageId: string | number): AxiosPromise<RoleMenuTree> => {
        return request({
            url: '/system/menu/tenantPackageMenuTreeselect/' + packageId,
            method: 'get'
        });
    };

// 新增菜单
    static addMenu = (data: MenuForm) => {
        return request({
            url: '/system/menu',
            method: 'post',
            data: data
        });
    };

// 修改菜单
    static updateMenu = (data: MenuForm) => {
        return request({
            url: '/system/menu',
            method: 'put',
            data: data
        });
    };

// 删除菜单
    static delMenu = (menuId: string | number) => {
        return request({
            url: '/system/menu/' + menuId,
            method: 'delete'
        });
    };

// 级联删除菜单
    static cascadeDelMenu = (menuIds: Array<string | number>) => {
        return request({
            url: '/system/menu/cascade/' + menuIds,
            method: 'delete'
        });
    };
}

/**
 * 菜单树形结构类型
 */
export interface MenuTreeOption {
    id: string | number;
    label: string;
    parentId: string | number;
    weight: number;
    children?: MenuTreeOption[];
}

export interface RoleMenuTree {
    menus: MenuTreeOption[];
    checkedKeys: string[];
}

/**
 * 菜单查询参数类型
 */
export interface MenuQuery {
    keywords?: string;
    menuName?: string;
    status?: string;
}

/**
 * 菜单视图对象类型
 */
export interface MenuVO extends BaseEntity {
    parentName: string;
    parentId: string | number;
    children: MenuVO[];
    menuId: string | number;
    menuName: string;
    orderNum: number;
    path: string;
    component: string;
    queryParam: string;
    isFrame: string;
    isCache: string;
    menuType: MenuTypeEnum;
    visible: string;
    status: string;
    icon: string;
    remark: string;
}

export interface MenuForm {
    parentName?: string;
    parentId?: string | number;
    children?: MenuForm[];
    menuId?: string | number;
    menuName: string;
    orderNum: number;
    path: string;
    component?: string;
    queryParam?: string;
    isFrame?: string;
    isCache?: string;
    menuType?: MenuTypeEnum;
    visible?: string;
    status?: string;
    icon?: string;
    remark?: string;
    query?: string;
    perms?: string;
}
